﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Configuration;
namespace Sinacor.Infra.Tools.LanguageCompiler
{
    public class Compile
    {
        private string pageNode = "    <Page Include=\"[INCLUDE_XAML]\">" + "\r\n" +
                              "      <Generator>MSBuild:Compile</Generator>" + "\r\n" +
                              "      <SubType>Designer</SubType>" + "\r\n" +
                              "    </Page>" + "\r\n";

        private string projectFolder = Application.StartupPath + @"\Languages\";
        private string projectName = "Languages.csproj";
        private string dllName = "Sinacor.UI.Application.View.SmartClient.Languages.dll";

        public void CompileLanguageProject()
        {
            CompileLanguageProject(false,null);
        }

        public void CompileLanguageProject(bool addFilesToProject, List<string> filesCreated)
        {
            if (addFilesToProject)
                AddFilesToProject(filesCreated);

            System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(projectFolder + "Compile.bat");
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = processStartInfo;

            bool ret = process.Start();
            process.WaitForExit();
            
            CopyLanguageDll();
        }

        private void CopyLanguageDll()
        {
            try
            {
                if (!Directory.Exists(ConfigurationSettings.AppSettings["languageTeamSystemFolder"].ToString()))
                    Directory.CreateDirectory(ConfigurationSettings.AppSettings["languageTeamSystemFolder"].ToString());

                File.Copy(projectFolder + "obj\\" + dllName, ConfigurationSettings.AppSettings["languageTeamSystemFolder"].ToString() + dllName,true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void AddFilesToProject(List<string> filesCreated)
        {

            XmlDocument xml = new XmlDocument();
            xml.Load(projectFolder + projectName);

            //Limpa todos os arquivos e folders do projeto
            xml.ChildNodes[1].ChildNodes[4].RemoveAll();
            xml.ChildNodes[1].ChildNodes[5].RemoveAll();

            foreach(string file in filesCreated)
            {
                XmlDocumentFragment page = xml.CreateDocumentFragment();
                page.InnerXml = pageNode.Replace("[INCLUDE_XAML]", file);

                xml.ChildNodes[1].ChildNodes[4].InnerXml = xml.ChildNodes[1].ChildNodes[4].InnerXml + pageNode.Replace("[INCLUDE_XAML]", file);

            }

            xml.Save(projectFolder + projectName);
        }
    }
}
